package com.aaa.controller.system;

import com.aaa.controller.BaseController;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.system.Dept;
import com.aaa.entity.Result;
import com.aaa.service.system.DeptService;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 部门表(Dept)表控制层
 *
 * @author makejava
 * @since 2022-08-30 20:07:34
 */
@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private DeptService deptService;

    /**
     * @param pageSortEntity
     * @param dept
     * @return
     * @create by: 张恒
     * @description: 查询
     * @create time: 2022/9/2 21:20
     */

    @RequestMapping("queryAllDeptPage.do")
    public Result queryAllDeptPage(PageSortEntity pageSortEntity, Dept dept) {
        Page page = deptService.queryDeptPage(pageSortEntity, dept);
        return success(page.getResult(), page.getTotal());
    }

    /**
     * @param
     * @return
     * @create by: 张恒
     * @description: 查询所有，不带分页，实体类
     * @create time: 2022/9/3 9:31
     */
    @RequestMapping("queryAllDept.do")
    public Result queryAllDept() {
        List<Dept> deptList = deptService.queryAllDept();
        return success(deptList);
    }


    /**
     * @param dept
     * @return
     * @create by: 张恒
     * @description: 添加
     * @create time: 2022/9/2 21:23
     */
    @RequestMapping("saveDept.do")
    public Result saveDept(Dept dept) {
        boolean b = deptService.insertDept(dept);
        if (b) {
            return success();
        } else {
            return fail();
        }
    }

    /**
     * @param id
     * @return
     * @create by: 张恒
     * @description: 删除
     * @create time: 2022/9/2 21:26
     */
    @RequestMapping("deleteDept.do")
    public Result deleteDept(Integer[] id, Integer deptId) {
        Integer integer = deptService.selectDeptUser(deptId);
        if (integer == 0) {
            boolean b = deptService.deleteDeptById(id);
            if (b) {
                return success();
            } else {
                return fail();
            }
        } else {
            return fail("部门中有人员，无法删除");
        }
    }

    /**
     * @param dept
     * @return
     * @create by: 张恒
     * @description: 修改
     * @create time: 2022/9/2 21:27
     */
    @RequestMapping("updateDept.do")
    public Result updateDept(Dept dept) {
        boolean b = deptService.updateDept(dept);
        if (b) {
            return success();
        } else {
            return fail();
        }
    }

    /**
     * @param dept
     * @return
     * @create by: 张恒
     * @description: 唯一校验
     * @create time: 2022/9/2 21:29
     */
    @GetMapping("repetitionName.do")
    public Result repetitionName(Dept dept) {
        boolean b = deptService.repetitionName(dept);
        if (b) {
            return success("部门名称可用");
        } else {
            return fail("部门名称重复或为空,不可用");
        }
    }
}

